package com.example.administrator.biyesheji_print.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.example.administrator.biyesheji_print.MainActivity;
import com.example.administrator.biyesheji_print.R;
import com.example.administrator.biyesheji_print.Utils.Constant;
import com.example.administrator.biyesheji_print.Utils.GlideImageLoader;
import com.example.administrator.biyesheji_print.Utils.SharedPreferencesUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2018/3/28.
 */

public class HomeFragment extends Fragment {
    List<Integer> marraylist;
    @BindView(R.id.iv_normal)
    ImageView ivNormal;
    @BindView(R.id.iv_color)
    ImageView ivColor;
    @BindView(R.id.iv_picture)
    ImageView ivPicture;
    Unbinder unbinder;

    private int img[] = {R.drawable.android_home_1, R.drawable.appmain_subject_1};
    private Banner banner;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//        每次FragmentTabHost切换fragment时会调用onCreateView()重绘UI。
//        解决方法，在fragment onCreateView 里缓存View
        if (view==null){
            view = inflater.inflate(R.layout.fragment_home, null);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent!=null){
            parent.removeView(view);
        }

        initData();
        initView(view);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }


    /**
     * 轮播图数据加载
     */
    private void initData() {
        marraylist = new ArrayList<Integer>();
        for (int i = 0; i < img.length; i++) {
            marraylist.add(img[i]);
        }
    }

    /**
     * @param view fragment-view视图
     *             加载轮播图及UI控件
     */
    private void initView(View view) {
        banner = view.findViewById(R.id.banner);
        setBanner();


    }

    private void setBanner() {
        //banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(marraylist);//轮播图片集合
        banner.isAutoPlay(false);
        banner.setDelayTime(3000);//轮播时间
        banner.setIndicatorGravity(BannerConfig.RIGHT);//指示器方向
        banner.setBannerAnimation(Transformer.DepthPage);//banner切换动画
        banner.start();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.iv_normal, R.id.iv_color, R.id.iv_picture})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_normal:
                SharedPreferencesUtils.putStringValue(getContext(), Constant.TYPE,"黑白打印");
                SharedPreferencesUtils.putBoolean(getContext(),"black",true);
                SharedPreferencesUtils.putBoolean(getContext(),"color",false);
                SharedPreferencesUtils.putBoolean(getContext(),"photo",false);
                MainActivity.tabhost.setCurrentTab(1);//tabhost切换
                break;
            case R.id.iv_color:
                SharedPreferencesUtils.putStringValue(getContext(), Constant.TYPE,"彩色打印");
                SharedPreferencesUtils.putBoolean(getContext(),"black",false);
                SharedPreferencesUtils.putBoolean(getContext(),"color",true);
                SharedPreferencesUtils.putBoolean(getContext(),"photo",false);

                MainActivity.tabhost.setCurrentTab(1);//tabhost切换
                break;
            case R.id.iv_picture:
                SharedPreferencesUtils.putStringValue(getContext(), Constant.TYPE,"照片打印");
                SharedPreferencesUtils.putBoolean(getContext(),"black",false);
                SharedPreferencesUtils.putBoolean(getContext(),"color",false);
                SharedPreferencesUtils.putBoolean(getContext(),"photo",true);

                MainActivity.tabhost.setCurrentTab(1);//tabhost切换
                break;
        }
    }
}
